package ContainerLayer;
import java.util.ArrayList;

/**
 * Write a description of class OrderContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Order> orders;
    private static OrderContainer instance;
    private ProductContainer pCon;    
    private long id;


    /**
     * Constructor for objects of class OrderContainer
     */
    public OrderContainer()
    {
        // initialise instance variables
        orders = new ArrayList<Order>();
        pCon = ProductContainer.getInstance();        
    }

    public static OrderContainer getInstance()
    {
        if(instance == null)
        {
            instance = new OrderContainer();
        }
        return instance;       
    }

    public long getID()
    {
        return id;
    }

    public void addOrder(Order order)
    {

        if(!orders.equals(order))
        {
            orders.add(order);
        }
        else 
        {
            System.out.println("There is already a order with the typed name");
        } 

    }

    public Order readOrder(long id)
    {
        int index = 0; 
        int orderIndex = 0;
        boolean found = false;

        while(index < orders.size() && !found)
        {
            if(orders.get(index).getID() == id)
            {                
                found = true; 
                orderIndex = index;
            }
            index++;
        }
        if(found)
        {
            return orders.get(orderIndex);
        }

        else
        {
            return null;
        }
    }

    public void updateOrder(long id, String dateModify, boolean done, Costumer costumerName, Salesman salesmanName)
    {
        int index = 0;
        int orderIndex = 0;
        boolean found = false;

        while(index < orders.size() && !found)
        {
            if(orders.get(index).getID() == id)
            {
                found = true;
                orderIndex = index;
            }          
            index++;
        }

        //If found, the filled parametre will be set
        // if not, the unfilled parametre will not be set
        if(found)
        {
            Order o = orders.get(orderIndex);
            //             if(date != null)
            //             {
            //                 o.setDate(date);
            //             }
            if(dateModify != null)
            {
                o.setDateModify(dateModify);
            }
            if(done != false)
            {
                o.setDone(done);
            }
            if(costumerName != null)
            {
                o.setCostumerName(costumerName);
            }
            if(salesmanName != null)
            {
                o.setSalesmanName(salesmanName);
            }
            //             if(product != null)
            //             {
            //                 o.setProduct(product);
            //             }
        }

        else
        {
            System.out.println("There is no order with the typed ID,  - try again with another ID");
        }
    }

    public void deleteOrder(long id)
    {   
        int index = 0;
        boolean deleted = false;
        while(index < orders.size() && !deleted)
        {
            if(orders.get(index).getID() == id)
            {
                orders.remove(index);
                deleted = true;
            }
            index++;                       
        }       

        if(!deleted)
        {
            System.out.println("There is no order with the typed ID, - try again with another ID");
        }

    }

    public void printOutAllOrder()
    {
        System.out.println("    SHOWS INFORMATION ABOUT ALL REGISTERED Salesmen");
        if(orders != null)
        {
            for(Order o : orders)
            {
                System.out.println("############################################################");
                System.out.println("#     Order ID: "+o.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        date: "+o.getDate());
                System.out.println("#  dateModify: "+o.getDateModify());
                System.out.println("#        done: "+o.getDone());
                System.out.println("#CostumerName: "+o.getCostumerName());
                System.out.println("#SalesmanName: "+o.getSalesmanName());                
                System.out.println("#");                
                System.out.println("########   THE END  for order ID: "+o.getID());
                System.out.println("");                
            }
        }
        else
        {
            System.out.println("           >>There is no order registred<<");
            System.out.println();
        }
    }

    public void printOneOrder(long id)
    {
        int index = 0; 
        int orderIndex = 0;
        boolean found = false;

        while(index < orders.size() && !found)
        {
            if(orders.get(index).getID() == id)
            {                
                found = true; 
                orderIndex = index;
            }
            index++;
        }
        Order o = orders.get(orderIndex);
        if(found)
        {
            System.out.println("############################################################");
            System.out.println("#     Order ID: "+o.getID());
            System.out.println("#  ---------------------------------------------------------");
            System.out.println("#        date: "+o.getDate());
            System.out.println("#  dateModify: "+o.getDateModify());
            System.out.println("#        done: "+o.getDone());
            System.out.println("#CostumerName: "+o.getCostumerName());
            System.out.println("#SalesmanName: "+o.getSalesmanName());                
            System.out.println("#");                
            System.out.println("########   THE END  for order ID: "+o.getID());
            System.out.println("");  
        }

        else
        {
            System.out.println("           >>There is no order with the typed ID: "+id+"<<");
            System.out.println();
        }

    }

    public int getSizeOrder()
    {
        if(orders != null)
        {
            return orders.size();
        }
        else
        {
            return 0;
        }

    } 

    public void createPartOrder(long orderID, long Pdescription, long Pno)
    {
        id++;
        Order o = readOrder(orderID);
        ProductDescription pd = pCon.readProductDescription(Pdescription);
        ProductNo pn = pCon.readProductNoToProductionDescription(Pdescription, Pno); 

        PartOrder po = new PartOrder(id, pd, pn);
        o.addPartOrder(po);       
    }

    public PartOrder readPartOrder(long orderID, long partID)
    {
        Order o = readOrder(orderID);
        return o.readPartOrder(partID);
    }

    public void updatePartOrder(long orderID, long partID, long Pdescription, long Pno)
    {
        Order o = readOrder(orderID);
        ProductDescription pd = pCon.readProductDescription(Pdescription);
        ProductNo pn = pCon.readProductNoToProductionDescription(Pdescription, Pno);
        
        o.updatePartOrder(partID, pd, pn);
    }

    public void deletePartOrder(long orderID, long partID)
    {
        Order o = readOrder(id);
        o.deletePartOrder(partID);
    }

    public void printOnePartOrderID(long orderID, long partID)
    {
        Order o = readOrder(orderID);
        o.printOnePartOrder(partID);
    }

    public void printAllPartOrder(long orderID)
    {
        Order o = readOrder(id);
        o.printAllPartorder();
    }


}
